package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lanhaif
 * @since 2022-08-23
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapper1=new QueryWrapper<>();
        wrapper1.eq("course_id",courseId);
        List<EduChapter> list1 = this.list(wrapper1);

        QueryWrapper<EduVideo> wrapper2=new QueryWrapper<>();
        wrapper2.eq("course_id",courseId);
        List<EduVideo> list2 = eduVideoService.list(wrapper2);

        List<ChapterVo> finalList=new ArrayList<>();

        for (int i = 0; i < list1.size(); i++) {
            EduChapter eduChapter = list1.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);


        List<VideoVo> videoVoList=new ArrayList<>();

        for(int j=0;j < list2.size();j++){
            EduVideo eduVideo=list2.get(j);
            if (eduVideo.getChapterId().equals(eduChapter.getId())){
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduVideo,videoVo);
                videoVoList.add(videoVo);
            }
        }
        chapterVo.setChildren(videoVoList);
    }
//        System.out.println(list1);
//        System.out.println(list2);
        return finalList;


    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            throw  new GuliException(20001,"含有小节不能删除");
        }else{
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }


}
